Skip to content

Conversation

@HeikoKlare
Copy link
Contributor

@HeikoKlare HeikoKlare commented Jan 6, 2025

The implementation of FindReplaceDocumentAdapter#findReplace(), used by its API methods #find() and #replace(), currently leaves an inconsistent state when failing because of an invalid input or state. This inconsistent state affects the last executed operation type, which the adapter stores in order to identify whether a replace operation is preceded by an according find operation. In case the #findReplace() method is called with an invalid position or to execute a replace without a preceding find, the method aborts (throwing an exception) without storing the operation to be executed as the last executed operation, i.e., it leaves the adapter as if that method was never called. In case the method is called in regular expression mode and the expression used as find or replace input is invalid, the operation aborts (throwing an exception) but still stores the operation to be executed as the last executed operation, i.e., it leaves the adapter as if that method was called successfully.

This behavior is unexpected as is handles invalid inputs inconsistently. This also becomes visible in the existing consumers, such as FindReplaceTarget#replaceSelection() used by the FindReplaceDialog and FindReplaceOvleray. They assume that in case an exception occurs while trying to perform a replace operation, a subsequent replace should succeed without an additional find operation being required. This assumption does currently not hold.

This change corrects the behavior of the FindReplaceDocumentAdapter#findReplace() method to always leave the adapter in an unmodified state when the method fails because of being called with invalid input or in an inconsistent state. In addition, regular expressions with an unfinished character escape at the end now lead to a proper exception.

Fixes #2657

How it behaves

This is how it behaves after the fix
findreplace_replace_regex_fix

The inconsistent error indication (input colored red) is specific to the FindReplaceOverlay and will be fixed in a follow-up PR.

Matcher replaceTextMatcher= pattern.matcher(prevMatch);
replaceText= replaceTextMatcher.replaceFirst(replaceText);
} catch (IndexOutOfBoundsException ex) {
} catch (IndexOutOfBoundsException | IllegalArgumentException ex) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary to fulfill the intention of interpretReplaceEscapes() to leave an unfinished character escape at the end of the pattern as is (such as in the pattern demo\) in order to produce a proper error message (which is delivered through an IllegalArgumentException from Matcher#replaceFirst()).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

such comment should be put in the code

Copy link
Contributor Author

@HeikoKlare HeikoKlare Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I have added a comment to the code explaining why these exceptions are caught in f020fbe.

@eclipse-platform-bot
Copy link
Contributor

eclipse-platform-bot commented Jan 6, 2025

This pull request changes some projects for the first time in this development cycle.
Therefore the following files need a version increment:

tests/org.eclipse.ui.workbench.texteditor.tests/META-INF/MANIFEST.MF

An additional commit containing all the necessary changes was pushed to the top of this PR's branch. To obtain these changes (for example if you want to push more changes) either fetch from your fork or apply the git patch.

Git patch
From 9284762b7c474e3a45565d7b5570414d2817925c Mon Sep 17 00:00:00 2001
From: Eclipse Platform Bot <[email protected]>
Date: Mon, 6 Jan 2025 13:17:02 +0000
Subject: [PATCH] Version bump(s) for 4.35 stream


diff --git a/tests/org.eclipse.ui.workbench.texteditor.tests/META-INF/MANIFEST.MF b/tests/org.eclipse.ui.workbench.texteditor.tests/META-INF/MANIFEST.MF
index 1a6ef95d92..05ec3b3086 100644
--- a/tests/org.eclipse.ui.workbench.texteditor.tests/META-INF/MANIFEST.MF
+++ b/tests/org.eclipse.ui.workbench.texteditor.tests/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
 Bundle-ManifestVersion: 2
 Bundle-Name: %Plugin.name
 Bundle-SymbolicName: org.eclipse.ui.workbench.texteditor.tests
-Bundle-Version: 3.14.600.qualifier
+Bundle-Version: 3.14.700.qualifier
 Bundle-Vendor: %Plugin.providerName
 Bundle-Localization: plugin
 Export-Package: 
-- 
2.47.1

Further information are available in Common Build Issues - Missing version increments.

@github-actions
Copy link
Contributor

github-actions bot commented Jan 6, 2025

Test Results

 1 821 files  ±0   1 821 suites  ±0   1h 37m 49s ⏱️ + 6m 41s
 7 738 tests +2   7 510 ✅ +3  228 💤 ±0  0 ❌  - 1 
24 375 runs  +6  23 626 ✅ +7  749 💤 ±0  0 ❌  - 1 

Results for commit 5cd7b18. ± Comparison against base commit 688f837.

♻️ This comment has been updated with latest results.

@HeikoKlare HeikoKlare marked this pull request as ready for review January 6, 2025 11:49
@HeikoKlare HeikoKlare requested a review from jukzi January 6, 2025 12:33
…pse-platform#2657

The implementation of FindReplaceDocumentAdapter#findReplace(), used by
its API methods #find() and #replace(), currently leaves an inconsistent
state when failing because of an invalid input or state.
This inconsistent state affects the last executed operation type, which
the adapter stores in order to identify whether a replace operation is
preceded by an according find operation. In case the #findReplace()
method is called with an invalid position or to execute a replace
without a preceding find, the method aborts (throwing an exception)
without storing the operation to be executed as the last executed
operation, i.e., it leaves the adapter as if that method was never
called. In case the method is called in regular expression mode and the
expression used as find or replace input is invalid, the operation
aborts (throwing an exception) but still stores the operation to be
executed as the last executed operation, i.e., it leaves the adapter as
if that method was called successfully.

This behavior is unexpected as is handles invalid inputs inconsistently.
This also becomes visible in the existing consumers, such as
FindReplaceTarget#replaceSelection() used by the FindReplaceDialog and
FindReplaceOvleray. They assume that in case an exception occurs while
trying to perform a replace operation, a subsequent replace should
succeed without an additional find operation being required. This
assumption does currently not hold.

This change corrects the behavior of the
FindReplaceDocumentAdapter#findReplace() method to always leave the
adapter in an unmodified state when the method fails because of being
called with invalid input or in an inconsistent state.
In addition, regular expressions with an unfinished character escape at
the end now lead to a proper exception.

Fixes #eclipse-platform#2657
Copy link
Contributor

@jukzi jukzi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets try

@HeikoKlare HeikoKlare merged commit b3aa5b0 into eclipse-platform:master Jan 6, 2025
17 checks passed
@HeikoKlare HeikoKlare deleted the issue-2657 branch January 6, 2025 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FindReplaceOverlay/FindReplaceDialog: correcting regexp does not work

3 participants